package cn.zcscloud.mall.service.impl;

import cn.zcscloud.mall.interceptor.UserHolder;
import cn.zcscloud.mall.mbg.dto.AdminMenuDto;
import cn.zcscloud.mall.mbg.mapper.UmsAdminMapper;
import cn.zcscloud.mall.mbg.mapper.UmsMenuMapper;
import cn.zcscloud.mall.mbg.mapper.UmsRoleMapper;
import cn.zcscloud.mall.mbg.mapper.UmsRoleMenuRelationMapper;
import cn.zcscloud.mall.mbg.model.UmsAdmin;
import cn.zcscloud.mall.mbg.model.UmsMenu;
import cn.zcscloud.mall.service.LoginService;
import cn.zcscloud.mall.utils.JwtUtils;
import cn.zcscloud.mall.utils.PasswordUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: LoginServiceImpl
 * Description:
 * date: 2022/3/31 14:07
 *
 * @author zjf95
 * @since JDK 1.8
 */
@Slf4j
@Service
public class LoginServiceImpl implements LoginService {


    @Autowired
    private UmsAdminMapper umsAdminMapper;

    @Autowired
    private UmsRoleMapper umsRoleMapper;

    @Autowired
    private UmsMenuMapper umsMenuMapper;

    @Autowired
    private UmsRoleMenuRelationMapper umsRoleMenuRelationMapper;


    @Override
    public Map login(String username, String password) {

        UmsAdmin umsAdmin = umsAdminMapper.selectOne(username);

        log.info("登录检验开始：{}", username);

        if (umsAdmin == null) {
            throw new RuntimeException("该用户不存在");
        }

        String adminPassword = umsAdmin.getPassword();

        //String salt = BCrypt.gensalt(10, new SecureRandom());
        //password = BCrypt.hashpw(password, salt);
        boolean verify = PasswordUtil.verify(password, adminPassword);
        Map resMap = new HashMap();

        if (verify) {

            Long userId = umsAdmin.getId();

            Map tokenMap = new HashMap<>();
            tokenMap.put("userId", userId);
            tokenMap.put("username", username);

            String token = JwtUtils.getToken(tokenMap);

            resMap.put("token", token);
            resMap.put("tokenHead","Bearer");

            return resMap;

        } else {

            resMap.put("message", "密码错误");
            return resMap;
        }

    }

    @Override
    public AdminMenuDto findByUserId(Long userId) {

        UmsAdmin umsAdmin = umsAdminMapper.selectByPrimaryKey(userId);

        String roles = umsRoleMapper.selectByUserId(userId);

        List<UmsMenu> umsMenus = umsMenuMapper.findByUserId(userId);

        AdminMenuDto adminMenuDto = new AdminMenuDto();

        adminMenuDto.setMenus(umsMenus);
        adminMenuDto.setIcon(umsAdmin.getIcon());
        adminMenuDto.setRoles(new String[]{roles});
        adminMenuDto.setUsername(umsAdmin.getUsername());

        return adminMenuDto;
    }

    /**
    * Description: 退出登录
    * @author: zjf95
    * @date: 2022/3/31 20:48
    * @param:
    * @return:
    */
    @Override
    public void logout() {

       UserHolder.remove();

    }
}
